package org.jeecg.modules.ywPalletBasic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.constant.CommonConstant;
import org.jeecg.modules.ywPalletBasic.entity.YwPalletInfo;
import org.jeecg.modules.ywPalletBasic.service.IYwPalletInfoService;
import org.jeecg.modules.ywSemiFinishedProductBasic.entity.YwSemiFinishedProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 托盘管理
 * @Author: jeecg-boot
 * @Date: 2024-08-27
 * @Version: V1.0
 */
@Api(tags = "托盘管理")
@RestController
@RequestMapping("/ywPalletBasic/ywPalletInfo")
@Slf4j
public class YwPalletInfoController extends JeecgController<YwPalletInfo, IYwPalletInfoService> {

    @Autowired
    private IYwPalletInfoService ywPalletInfoService;

    /**
     * 托盘列表
     * @author: zk
     * @date: 2024/8/15 10:57
     * @return:
     **/
    @AutoLog(value = "托盘管理-列表查询")
    @ApiOperation(value = "托盘管理-列表查询", notes = "托盘管理-列表查询")
    @PostMapping("/queryPalletList")
    public Result<?> queryProjectList() {
        List<YwPalletInfo> list = ywPalletInfoService.list(new QueryWrapper<YwPalletInfo>().eq("status", CommonConstant.StatusEnum.YES.getCode()));
        return Result.OK(list);
    }

    /**
     * 分页列表查询
     *
     * @param ywPalletInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "托盘管理-分页列表查询")
    @ApiOperation(value = "托盘管理-分页列表查询", notes = "托盘管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YwPalletInfo ywPalletInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<YwPalletInfo> queryWrapper = QueryGenerator.initQueryWrapper(ywPalletInfo, req.getParameterMap());
        queryWrapper.eq("status", CommonConstant.StatusEnum.YES.getCode());
        Page<YwPalletInfo> page = new Page<YwPalletInfo>(pageNo, pageSize);
        IPage<YwPalletInfo> pageList = ywPalletInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param ywPalletInfo
     * @return
     */
    @AutoLog(value = "托盘管理-添加")
    @ApiOperation(value = "托盘管理-添加", notes = "托盘管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YwPalletInfo ywPalletInfo) {
        ywPalletInfo.setStatus(CommonConstant.StatusEnum.YES.getCode());
        ywPalletInfoService.save(ywPalletInfo);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param ywPalletInfo
     * @return
     */
    @AutoLog(value = "托盘管理-编辑")
    @ApiOperation(value = "托盘管理-编辑", notes = "托盘管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody YwPalletInfo ywPalletInfo) {
        ywPalletInfoService.updateById(ywPalletInfo);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "托盘管理-通过id删除")
    @ApiOperation(value = "托盘管理-通过id删除", notes = "托盘管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        YwPalletInfo palletInfo = ywPalletInfoService.getById(id);
        Assert.notNull(palletInfo, "托盘数据异常,请联系管理员！");
        palletInfo.setStatus(CommonConstant.StatusEnum.NO.getCode());
        ywPalletInfoService.updateById(palletInfo);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "托盘管理-批量删除")
    @ApiOperation(value = "托盘管理-批量删除", notes = "托盘管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> wantDeleteIdList = Arrays.asList(ids.split(","));
        List<YwPalletInfo> semiFinishedProductInfoList = ywPalletInfoService.list(new QueryWrapper<YwPalletInfo>()
                .in("id", wantDeleteIdList)
                .eq("status", CommonConstant.StatusEnum.YES.getCode()));
        Assert.isTrue(wantDeleteIdList.size() == semiFinishedProductInfoList.size(), "删除失败,请检查数据是否异常！");
        semiFinishedProductInfoList.forEach(data -> data.setStatus(CommonConstant.StatusEnum.NO.getCode()));
        this.ywPalletInfoService.updateBatchById(semiFinishedProductInfoList);
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "托盘管理-通过id查询")
    @ApiOperation(value = "托盘管理-通过id查询", notes = "托盘管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        YwPalletInfo ywPalletInfo = ywPalletInfoService.getById(id);
        Assert.notNull(ywPalletInfo, "托盘数据异常,请联系管理员！");
        return Result.OK(ywPalletInfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ywPalletInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YwPalletInfo ywPalletInfo) {
        return super.exportXlsByStatus(request, ywPalletInfo, YwPalletInfo.class, "托盘管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YwPalletInfo.class);
    }

}
